package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j //使用Slf4j日志
@Api(tags = "员工相关接口")//注意：@Api注解使用tags定义说明，@ApiOperation注解使用value属性定义说明
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * @param employeeLoginDTO 数据传输对象
     * @return Result<EmployeeLoginVO>视图对象
     */
    @PostMapping("/login")
    @ApiOperation("员工登录接口")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        //调用服务层方法，获取对应Employee对象
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌（固定流程）
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());//用empId来生成token
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        //使用builder模式生成对象
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("登出接口")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工接口
     * @param employeeDTO employeeDTO
     * @return Result
     */
    @PostMapping
    @ApiOperation("新增员工接口")
    public Result add(@RequestBody EmployeeDTO employeeDTO) {
        //记录日志，展示employeeDTO对象
        log.info("新增员工：{}",employeeDTO);
        //Service层对用户进行插入
        //如果插入成功，才会继续往下执行。如果由于不符合username唯一性约束，会导致数据库报错，被全局异常处理捕捉，返回错误信息
        employeeService.add(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页查询接口
     * @param employeePageQueryDTO 用于接受前端使用param方式传递的值
     * @return Result
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询接口")
    //PageResult是标准的封装分页查询结果的类
    //注意代码规范！任何接口都要定义对应的VO和DTO
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询：{}",employeePageQueryDTO);
        PageResult data=employeeService.page(employeePageQueryDTO);
        return Result.success(data);
    }

    /**
     * 修改员工状态
     * @param status 路径参数，需要修改的员工状态
     * @param id param参数，需要修改的员工id
     * @return Result
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改员工状态接口")
    public Result changeStatus(@PathVariable("status") Integer status,
                               @RequestParam Long id) {
        log.info("修改员工{}状态为{}",id,status);
        employeeService.changeStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工具体信息
     * @param id 员工id
     * @return Result
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工具体信息接口")
    public Result<Employee> searchById(@PathVariable("id") Long id) {
        log.info("根据id{}查询员工具体信息",id);
        Employee employee=employeeService.searchById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     * @param employeeDTO 需要进行编辑的员工信息
     * @return Result
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result changeInfo(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息:{}",employeeDTO);
        employeeService.changeInfo(employeeDTO);
        return Result.success();
    }
}
